package com.lyeducation.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Base64;

@Slf4j
/** @author YangYi */
public class HttpUtils {
    /**
     * 从request获取登录的IP
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 判断是否为ajax请求
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        if (request.getHeader("accept").indexOf("application/json") > -1
                || (request.getHeader("X-Requested-With") != null
                && "XMLHttpRequest".equals(request.getHeader("X-Requested-With")))) {
            return true;
        }
        return false;
    }

    public static File setBase64Data(String packageId, String name, String data) {

//    String data = "....";
        String[] fileBase64 = data.split(",");
        String imageType = fileBase64[0].split(";")[0].split("/")[1];
        String base64Data = fileBase64[1];
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(base64Data);
        File packageFile = null;

        try {
            File file =
                    ResourceUtils.getFile("classpath:static/image");
            log.info("file:" + file.getPath());
            packageFile = new File(file.getPath() + "/" + packageId);
            if (!packageFile.exists()) {
                packageFile.mkdirs();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        FileOutputStream fos = null;
        try {
            String fileUrl = packageFile.getPath();
            File outPutFile = new File(fileUrl + "/" + name + "." + imageType);
            if (!outPutFile.exists()) {
                outPutFile.createNewFile();
            }
            fos = new FileOutputStream(outPutFile);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);
            bufferedOutputStream.write(bytes);
            return outPutFile;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


}
